import random
import numpy as np
import pandas as pd
import seaborn as sns
import networkx as nx
import plotly.express as px
import plotly.graph_objects as go
from collections import Counter as c
from matplotlib import pyplot as plt
%matplotlib inline
Freemans centrality equation:
$C=\frac{\sum_{i=1}^{N} C(p_*)-C(p_i)}{\max \sum_{i=1}^{N} C(p_*)-C(p_i)}$
For Freeman centrality consider degree centrality and for the number of nodes(n) the max degree centrality of a graph will be n-1.The max sum of the diff b/w the most central node and all the other will be for star topology. The max sum of difference will calculate using the equation:
$\sum_i^{n-1} (C_{max} - C_i) = \sum_i^{n-1} (n - 1 - 1) = -2n-2 \sum_i^{n-1} n = n^2-3n-2$
number = 50
number_of_nodes = list(range(number))
Graph = nx.Graph() # using networks library
Graph.add_nodes_from(number_of_nodes)
#The max sum of the difference between the most central node
maximum = number**2 - 3 * number - 2
graph_size = number * (number-1) / 2
freeman_list = []
while not len(Graph.edges()) == graph_size:
a, b = np.random.choice(number_of_nodes, size=2, replace=False)
if a in Graph[b]:
continue
Graph.add_edge(a, b)
centr = dict(nx.degree(Graph))
max_centr = max(centr.values())
freeman = sum(max_centr - np.array(list(centr.values()))) / maximum
freeman_list.append(freeman)
# Plot the result
fig = px.line(freeman_list)
fig.update_layout(
title = "Freemans centrality",
xaxis_title='edges',
yaxis_title='centrality')
It's depend upon various factor such as network properties, context and aim of the analysis.
There square measure many various measures of centrality.
The simplest approach would be to calculate the degree of a node. Another may well be shrewd the weighted add of neighbor's degrees. This approach focuses on the notion of being connected to different necessary nodes. this fashion of brooding about centrality is wide used on the globe wide net and social media platforms.
A different approach to centrality is termed 'betweenness'. It focuses on a question: however necessary is that this node in terms of traffic between all different nodes? Nodes with high betweenness centrality have influence within the network by virtue of their management over data passing between others.
They have plenty of power: their removal would disrupt communication. This will be helpful in graphs representing traffic, e.g. causation information packages.
Another one is 'closeness' centrality. It answers the question: however, on average, this node is near to all different nodes (in terms of the shortest path).
This is helpful for instance in social media networks, wherever this will show what proportion it takes one person to attach to others.
loc = 'C:\\Users\\amir\\Data Science\\Data Exploration and Visualization\\Homework\\Homework 5\\Data\\drosophhila_net'
dataset = pd.read_csv(loc, delimiter='\t')
dataset.tail(3)
G = nx.Graph()
edges = dataset.loc[:, ['FBGN_GENE1_BD', 'FBGN_GENE2_AD']].apply(list, axis=1).tolist()
G.add_edges_from(edges)
n = len(G.nodes())
plt.figure(figsize=(14,14))
nx.draw(G, pos=nx.spiral_layout(G), alpha =0.7, node_size=25, node_color='blue')
degrees = dict(nx.degree(G))
# Histogram
px.histogram(degrees.values(), range_x=(0,30))
# Scatter Plot
distribution = {degree: count/n for (degree, count) in c(degrees.values()).items()}
px.scatter(x=distribution.keys(), y=distribution.values(), log_y=True, log_x=True)
# Here is Top 10 Value of Degree, Page Rank and EigenVector
def get_10_best(nodes_scores):
return sorted(nodes_scores.items(), key=lambda x: x[1], reverse=True)[:10]
d = get_10_best(degrees)
print('Degree', d)
p = get_10_best(nx.pagerank(G))
print('Page Rank', p)
e = get_10_best(nx.eigenvector_centrality(G))
print('Eigen Vector', e)



